package cn.pugle.base.net.interfa;

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * @author tzp
 * @since 2018/11/30
 */
public class NI {
    public static void main(String[] args) throws SocketException {
        displayInterfaces();
    }

    public static void displayInterfaces() throws SocketException {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface networkInterface = interfaces.nextElement();
            System.out.println(networkInterface.toString());
            System.out.println("网口地址");
            for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
                System.out.println(interfaceAddress);
            }

            System.out.println("网口绑定的ip地址");
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                System.out.println(inetAddress + " " + inetAddress.getHostName());
            }

            System.out.println("-----");
        }
    }
}
